package giaodien;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.NumberFormat;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import controller.KhachHangController;

public class SuaKhachHang extends JFrame {

	private JPanel contentPane;
	private JTextField textField;
	private JTextField textField_2;
	private JTextField textField_3;
	private JTextField textField_4;
	private JTextField textField_5;
private NumberFormat n = NumberFormat.getInstance();
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					SuaKhachHang frame = new SuaKhachHang();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public SuaKhachHang() {
		setTitle("Sửa thông tin khách hàng");
		//setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 452, 395);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		JLabel lblNhpMKhch = new JLabel(
				"Nh\u1EADp m\u00E3 kh\u00E1ch h\u00E0ng c\u1EA7n s\u1EEDa");
		lblNhpMKhch.setForeground(SystemColor.textHighlight);
		lblNhpMKhch.setBounds(10, 37, 181, 14);
		contentPane.add(lblNhpMKhch);

		JLabel lblMKhchHng = new JLabel("Mã khách hàng");
		lblMKhchHng.setBounds(12, 113, 112, 16);
		contentPane.add(lblMKhchHng);

		final JLabel label = new JLabel("");
		label.setBounds(134, 113, 112, 16);
		contentPane.add(label);

		JLabel lblTnKhchHng = new JLabel("Tên khách hàng ");
		lblTnKhchHng.setBounds(10, 149, 112, 16);
		contentPane.add(lblTnKhchHng);

		textField_2 = new JTextField();
		textField_2.setColumns(10);
		textField_2.setBounds(132, 147, 133, 20);
		contentPane.add(textField_2);

		JLabel lblaCh = new JLabel("Địa chỉ");
		lblaCh.setBounds(10, 183, 112, 16);
		contentPane.add(lblaCh);

		textField_3 = new JTextField();
		textField_3.setColumns(10);
		textField_3.setBounds(132, 181, 133, 20);
		contentPane.add(textField_3);

		JLabel lblSinThoi = new JLabel("Số điện thoại");
		lblSinThoi.setBounds(10, 217, 112, 16);
		contentPane.add(lblSinThoi);

		textField_4 = new JTextField();
		textField_4.setColumns(10);
		textField_4.setBounds(132, 214, 133, 20);
		contentPane.add(textField_4);

		JLabel lblimTchLy = new JLabel("Điểm tích lũy");
		lblimTchLy.setBounds(10, 254, 112, 16);
		contentPane.add(lblimTchLy);

		textField_5 = new JTextField();
		textField_5.setColumns(10);
		textField_5.setBounds(132, 252, 133, 20);
		contentPane.add(textField_5);

		textField_2.setVisible(false);
		textField_3.setVisible(false);
		textField_4.setVisible(false);
		textField_5.setVisible(false);

		// nhập mã khách hàng và nút OK
		textField = new JTextField();
		textField.setBounds(10, 64, 114, 23);
		contentPane.add(textField);
		textField.setColumns(10);

		final JLabel label_2 = new JLabel("");
		label_2.setForeground(Color.RED);
		label_2.setBounds(279, 183, 144, 16);
		contentPane.add(label_2);

		final JLabel label_3 = new JLabel("");
		label_3.setForeground(Color.RED);
		label_3.setBounds(278, 217, 145, 16);
		contentPane.add(label_3);

		JButton btnNewButton = new JButton("OK");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				try {
					int id = Integer.parseInt(textField.getText());
					model.KhachHang kh = KhachHangController.getKHachHang(id);
					int maKH = kh.getMaKH();
					String tenKH = kh.getTenKH();
					String diaChi = kh.getDiaChi();
					String sdt = kh.getSdt();
					double diem = kh.getDiemTichLuy();
					//định dạng sang String
					String d = n.format(diem);
					label.setText(Integer.toString(maKH));
					// khi tạo ra nó ẩn h cho nó hiện ra
					textField_2.setVisible(true);
					// set dữ liệu cho nó (textField_2)
					textField_2.setText(tenKH);
					textField_3.setVisible(true);
					textField_3.setText(diaChi);
					textField_4.setVisible(true);
					textField_4.setText(sdt);
					textField_5.setVisible(true);
					textField_5.setText(d);
					label_2.setText("");
					label_3.setText("");

				} catch (Exception e) {
					label_2.setText("Mã khách hàng không");
					label_3.setText("đúng. Vui lòng nhập lại!");
					textField.setText("");
				}
			}
		});
		btnNewButton.setBounds(134, 64, 63, 23);
		contentPane.add(btnNewButton);

		JLabel label_1 = new JLabel("");
		label_1.setIcon(new ImageIcon(SuaKhachHang.class.getResource("/icon/Edit-Male-User-icon.png")));
		label_1.setBounds(301, 89, 86, 89);
		contentPane.add(label_1);

		JButton btnSa = new JButton("Sửa");
		btnSa.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					model.KhachHang kh = new model.KhachHang();
					kh.setMaKH(Integer.parseInt(label.getText()));
					kh.setTenKH(textField_2.getText());
					kh.setDiaChi(textField_3.getText());
					kh.setSdt(textField_4.getText());
					kh.setDiemTichLuy(Double.parseDouble(textField_5.getText()));
					KhachHangController.updateKhachHang(kh);
					label_2.setText("");
					label_3.setText("");
					textField.setText("");
					JOptionPane.showMessageDialog(null,
							"Sửa thông tin khách hàng thành công");
				} catch (Exception e2) {
					JOptionPane.showMessageDialog(null, "Đã có lỗi xảy ra vui lòng kiểm tra lại");
				}
			}
		});
		btnSa.setBounds(104, 291, 92, 26);
		contentPane.add(btnSa);

		JButton button_1 = new JButton("Hủy");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				dispose();
			}
		});
		button_1.setBounds(215, 291, 77, 26);
		contentPane.add(button_1);

	}
}
